home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0002_MCGATUT.TXT.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  5KB  |  181 lines

  1.                            MCGA Graphics Tutorial
  2.                                  Lesson #1
  3.                                 by Jim Cook
  4.  
  5. I'm not sure how this online tutorial will be received, but with your
  6. comments and feedback I plan on creating a full-blown animation package. This
  7. graphics library will be available to the public domain and will contain the
  8. following abilities:
  9.  
  10.                 Setting/Reading Pixels
  11.                 Drawing lines
  12.                 Saving/Restoring areas of the screen
  13.                 Displaying PCX/LBM files to the screen
  14.                 Spriting (Display picture with transparent areas)
  15.                 Palette control (Smooth fades to black)
  16.                 Page flipping
  17.  
  18. Before we're done, you will have the tools to produce programs with rich,
  19. even photo-realistic (for the resolution) images on your PC.  The necessary
  20. hardware is a VGA card and monitor that's it.  I'll be using Turbo Pascal
  21. version 6.0.  Please holler if that will be a problem.  I'm using it to
  22. create inline assembly.  My alternatives are inline code (yuk) or linking in
  23. external assembly.  For speed (and actually ease) the latter is better.  If I
  24. receive three complaints against 6.0, I'll use external assembly.
  25.  
  26.                                 What is MCGA?
  27.  
  28. Multi-Color Graphics Array is the video card that IBM built into it's Model
  29. 25 and 30 PS/2's.  It subsequently became a subset of the standard VGA
  30. adapter card.  It has the distiction of being the first card (excluding
  31. Targa and other expensive cards) to display 256 colors at once on the
  32. computer screen.  To us that meant cool games and neat pictures.  The MCGA
  33. addapter has added two new video modes to the PC world:
  34.  
  35.                 Mode $11        640x480x2 colors
  36.                 Mode $13        320x200x256 colors
  37.  
  38. Obviously, we will deal with mode $13.  If we wanted to deal with two
  39. colors, we'd be programming a CGA.  So much for the history lesson...let's
  40. dive in.
  41.  
  42. I've created a unit, MCGALib, that will contain all of our MCGA routines.
  43. The first two procedures we will concern ourselves with are setting the
  44. graphics mode and setting a pixel.  The MCGALib is followed by a test
  45. program that uses the two procedures:
  46.  
  47. Unit MCGALib;
  48.  
  49. interface
  50.  
  51. Procedure SetGraphMode (Num:Byte);
  52. Procedure SetPixel     (X,Y:Integer;Color:Byte);
  53.  
  54. implementation
  55.  
  56. var
  57.   ScreenWide  :  Integer;
  58.   ScreenAddr  :  Word;
  59.  
  60. Procedure SetGraphMode (Num:Byte);
  61. begin
  62.   asm
  63.     mov al,Num
  64.     mov ah,0
  65.     int 10h
  66.     end;
  67.   Case Num of
  68.     $13 : ScreenWide := 320;
  69.     end;
  70.   ScreenAddr := $A000;
  71. end;
  72. {
  73. Function PixelAddr (X,Y:Word) : Word;
  74. begin
  75.   PixelAddr := Y * ScreenWide + X;
  76. end;
  77.  
  78. Procedure SetPixel (X,Y:Integer;Color:Byte);
  79. var
  80.   Ofs    :  Word;
  81. begin
  82.   Ofs := PixelAddr (X,Y);
  83.   Mem [ScreenAddr:Ofs] := Color;
  84. end;
  85. }
  86.  
  87. Procedure SetPixel (X,Y:Integer;Color:Byte);
  88. begin
  89.   asm
  90.     push ds
  91.     mov  ax,ScreenAddr
  92.     mov  ds,ax
  93.  
  94.     mov  ax,Y
  95.     mov  bx,320
  96.     mul  bx
  97.     mov  bx,X
  98.     add  bx,ax
  99.  
  100.     mov  al,Color
  101.     mov  byte ptr ds:[bx],al
  102.     pop  ds
  103.     end;
  104. end;
  105.  
  106. Begin
  107. End.
  108.  
  109. This is the test program to make sure it's working...
  110.  
  111. Program MCGATest;
  112.  
  113. uses
  114.   Crt,Dos,MCGALib;
  115.  
  116. var
  117.   Stop,
  118.   Start  :  LongInt;
  119.   Regs   :  Registers;
  120.  
  121. Function Tick : LongInt;
  122. begin
  123.   Regs.ah := 0;
  124.   Intr ($1A,regs);
  125. = egs.cx hl 16  Rgs.dx;
  126. end;
  127.  
  128. Procedure Control;
  129. var
  130.   I,J :  Integr;begin
  131.   Start := ic;
  132.   Fr I := 0 to 199 do
  133.   For J  SetPixe (J,I,Random(256));
  134.  Stop := Tick;
  135. end;
  136.  
  137. Pocdure Closing;
  138. var
  139.   Ch    :  Chr;
  140. begin
  141.   Repet Until Keypressed;
  142.   While Keypressed do Ch:= Reake;
  143.   TextMode (3);
  144. ook '(Stop-Start),' ticks or ,(Stop-Start)/182:4:3,'
  145.  seconds!');
  146. nd;
  147.  
  148. Procedure Init;
  149. begin
  150.   SetGaphMode ($13);
  151.  Randoiz;
  152. end;
  153.  
  154. Begin
  155.  Init
  156.   Control;
  157.   Cosing;
  158. e where these listings coul get unbearably long in time.  I'l
  159. explore a few ays I can get this information to ya'll without takingup too
  160. much pace. Iwould like you tomake sue this routine works, ust in case
  161. you ou graphis card. You may notce two SetPxel
  162. procedures in the MCGALib, one is commented out.  Remove he comments,
  163. comment up the uncommented SetPixel and run the test program aain.  Notice
  164. the speed degradation.  Linking in raw assembly will eve improve upon the
  165. speed of the inline assembly.
  166. Please take the time to study each procedure and ASK ANY QUESTIONS tht you
  167. may have, even if it doesn't relate to the graphics routines.  I'm cetain I
  168. do not want to get pulled off track by any discussions about STYLE,ur critique
  169.  for others to learn rom.
  170.  
  171.                               Coming next time
  172.  
  173. I think a discussio of video memory is paramount.  Possibly vertical and
  174. horizontal lines, if spce permits.
  175.  
  176. Happy grafx
  177. jim
  178.  
  179. --- QuickBBS 2.75
  180.  * Origin: Quantum Leap.. (512)333-5360  HST/DS (1:387/307)
  181.